//
//  FoodCell.m
//  shise
//
//  Created by 毛志鹏 on 14/12/17.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "FoodCell.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "HomeViewController.h"

@implementation FoodCell

- (void)dealloc
{
    [_foodPic release];
    [_food release];
    [_userHead release];
    [_userName release];
    [_location release];
    [_content release];
    [_like release];
    [_collect release];
    [_collectIcon release];
    [_likeIcon release];
    [_likeCount release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.foodPic = [UIButton buttonWithType:UIButtonTypeCustom];
        self.foodPic.backgroundColor = [UIColor purpleColor];
//        [self.foodPic addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.foodPic];
        // [_foodPic release];
        
        self.userHead = [UIButton buttonWithType:UIButtonTypeCustom];
        self.userHead.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.userHead];
        
//        [self.userHead addTarget:self action:@selector(dianji) forControlEvents:UIControlEventTouchUpInside];
        
        self.userName = [[UILabel alloc] init];
//        self.userName.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:self.userName];
        [_userName release];
        
        self.location = [[UILabel alloc] init];
        self.location.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:self.location];
        [_location release];
        
        self.content = [[UILabel alloc] init];
        self.content.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:self.content];
        [_content release];
        
        self.collect = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.collect setTitle:@"收一个" forState:UIControlStateNormal];
        self.collect.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.collect];
//        [self.collect addTarget:self action:@selector(shoucang) forControlEvents:UIControlEventTouchUpInside];
        
        
        self.like = [UIButton buttonWithType:UIButtonTypeSystem];
        self.like.backgroundColor = [UIColor whiteColor];
        [self.like setTitle:@"赞一个" forState:UIControlStateNormal];
        [self.contentView addSubview:self.like];
//        [self.like addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
        
        self.likeCount = [[UILabel alloc] init];
        [self.contentView addSubview:self.likeCount];
        [_likeCount release];
        
        self.collectIcon = [[UIImageView alloc] init];
        self.collectIcon.image = [UIImage imageNamed:@"iconfont-shoucang.png"];
        [self.contentView addSubview:self.collectIcon];
        [_collectIcon release];
        
        self.likeIcon = [[UIImageView alloc] init];
        self.likeIcon.image = [UIImage imageNamed:@"iconfont-zan.png"];
        [self.contentView addSubview:self.likeIcon];
        [_likeIcon release];
    
        
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _foodPic.frame = CGRectMake(10, 10, self.contentView.frame.size.width - 20, 334);
    
    _userHead.frame = CGRectMake(20, 325, 35, 35);
    _userHead.imageView.layer.cornerRadius = 16;

    _userName.frame = CGRectMake(65, 345, 100, 20);
    _userName.font = [UIFont boldSystemFontOfSize:13];
    
    _location.frame = CGRectMake(220, 345, 135, 20);
    _location.textAlignment = NSTextAlignmentRight;
    _location.font = [UIFont boldSystemFontOfSize:13];
    
    _content.frame = CGRectMake(20, 368, self.contentView.frame.size.width - 70, 100);
    _content.numberOfLines = 3;
    _content.font = [UIFont boldSystemFontOfSize:14];
    [_content sizeToFit]; // 自适应高度
    
    
    _like.frame = CGRectMake(20, self.contentView.frame.size.height - 30, 150, 30);
    _likeIcon.frame = CGRectMake(30, self.contentView.frame.size.height - 30, 30, 30);
    _likeCount.frame = CGRectMake(130, self.contentView.frame.size.height - 30, 50, 30);
    
    
    _collect.frame = CGRectMake(205, self.contentView.frame.size.height - 30, 150, 30);
    _collectIcon.frame = CGRectMake(215, self.contentView.frame.size.height - 30, 30, 30);
    
}


// 重写Food setter方法
- (void)setFood:(Food *)food
{
    if (_food != food) {
        [_food release];
        _food = [food retain];
    }
    [_foodPic setImageWithURL:[NSURL URLWithString:_food.pic_url]];
    NSDictionary *userDic = [NSDictionary dictionaryWithDictionary:_food.user];
    [_userHead setImageWithURL:[NSURL URLWithString:[userDic objectForKey:@"avatar"]]];
    
    _userName.text = _food.userName;
    _location.text = _food.location;
//    NSLog(@"点赞人数: %@", _food.likeCount);
    _likeCount.text = [NSString stringWithFormat:@"%@", _food.likeCount];
    _content.text = _food.content;
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
